package com.eystar.pdc.directive;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eystar.pdc.model.DicItem;
import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.expr.ast.Expr;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

/**
 * <pre>
 * 获取字典code的值，返回的是json对象
 * #dic_json("code1","code2")
 * 输出结果为 {code1:{key1:value1,key2:value2},code2:{key1:value1,key2:value2}}
 * 表示 字典1：{字典项}， 字典2:{字典项}
 * </pre>
 * @author Administrator
 *
 */
public class DicJsonDirective extends Directive {
	
	@Override
	public void exec(Env env, Scope scope, Writer writer) {
		JSONObject object = new JSONObject();
		for (Expr expr : exprList.getExprArray()) {
			String code = (String) expr.eval(scope);
			Map<String, String> items = DicItem.dao.findDicItems(code);
			object.put(code, JSON.toJSON(items));
		}
		write(writer, object.toJSONString());
	}

}
